/*
 * color.c
 *
 *  Created on: Apr 2, 2012
 *      Author: Tri_D
 *  Using the provided values from serial input to change background color.
 *  
 */

#include "color.h"
#include "math.h"
#include "main header.h"
#include <mc9s08ll64.h>

void Turn_Off_Red(void)
{
	RED_CONTROL_PIN = 0;
}

void Turn_Off_Blue(void)
{
	BLUE_CONTROL_PIN = 0;
}

void Turn_Off_Green(void)
{
	GREEN_CONTROL_PIN = 0;
}

void Turn_On_Red(void)
{
	Turn_Off_Blue();
	Turn_Off_Green();
	RED_CONTROL_PIN = 1;
}


void Turn_On_Blue(void)
{
	Turn_Off_Red();
	Turn_Off_Green();
	BLUE_CONTROL_PIN = 1;
}


void Turn_On_Green(void)
{
	Turn_Off_Red();
	Turn_Off_Blue();
	GREEN_CONTROL_PIN = 1;	
}


// red = 0 - 255
void process_red(uint8_t red)
{
	float_t percent_ = 0;
	uint16_t red_ = 0;
	
	// Adjust red tone
	percent_ = (float_t) ((float_t) (MAX_COLOR_VAL-red)/(float_t) MAX_COLOR_VAL);
	red_ = (uint16_t) (percent_ * PERIOD_COUNT_DUTY_CYCLE);
	// Change red
	(void)(TPM1C1SC == 0U);              /* Channel 0 int. flag clearing (first part) */
	/* TPM1C1SC: CH1F=0,CH1IE=0,MS1B=1,MS1A=0,ELS1B=0,ELS1A=1 */
	TPM1C1SC = 0x24U;                    /* Int. flag clearing (2nd part) and channel 0 contr. register setting */
	TPM1C1V = red_;        				 /* Compare 0 value setting */
	/* TPM1SC: TOF=0,TOIE=0,CPWMS=0,CLKSB=0,CLKSA=0,PS2=0,PS1=0,PS0=0 */
	TPM1SC = 0x00U;                      /* Stop and reset counter */
	TPM1MOD = 0x04DDU;                   /* Period value setting */
	(void)(TPM1SC == 0U);                /* Overflow int. flag clearing (first part) */
	TPM1SC = 0x2BU;                      /* Int. flag clearing (2nd part) and timer control register setting */	
}


// blue = 0 - 255
void process_blue(uint8_t blue)
{
	float_t percent_ = 0;
	uint16_t red_ = 0, blue_ = 0, green_ = 0;	

	// Adjust Blue tone
	percent_ = (float_t) ((float_t) (255-blue) / (float_t) MAX_COLOR_VAL);
	blue_ =  (uint16_t) (percent_ * PERIOD_COUNT_DUTY_CYCLE);
		
	// Change blue
	(void)(TPM2C1SC == 0U);              /* Channel 1 int. flag clearing (first part) */
	/* TPM2C1SC: CH1F=0,CH1IE=0,MS1B=1,MS1A=0,ELS1B=0,ELS1A=1 */
	TPM2C1SC = 0x24U;                    /* Int. flag clearing (2nd part) and channel 1 contr. register setting */
	TPM2C1V = blue_;                   /* Compare 1 value setting */
	/* TPM2SC: TOF=0,TOIE=0,CPWMS=0,CLKSB=0,CLKSA=0,PS2=0,PS1=0,PS0=0 */
	TPM2SC = 0x00U;                      /* Stop and reset counter */
	TPM2MOD = 0x04DDU;                   /* Period value setting */
	(void)(TPM2SC == 0U);                /* Overflow int. flag clearing (first part) */
	/* TPM2SC: TOF=0,TOIE=0,CPWMS=1,CLKSB=0,CLKSA=1,PS2=0,PS1=1,PS0=1 */
	TPM2SC = 0x2BU;                      /* Int. flag clearing (2nd part) and timer control register setting */		
}

// green = 0 -255
void process_green(uint8_t green)
{
	float_t percent_ = 0;
	uint16_t red_ = 0, blue_ = 0, green_ = 0;
	
	// Adjust Green tone
	percent_ = (float_t) ((float_t) (255-green) / (float_t) MAX_COLOR_VAL);	
	green_ = (uint16_t) (percent_ * PERIOD_COUNT_DUTY_CYCLE);
	
	// Change green
	(void)(TPM2C0SC == 0U);              /* Channel 0 int. flag clearing (first part) */
	/* TPM2C0SC: CH0F=0,CH0IE=0,MS0B=1,MS0A=0,ELS0B=0,ELS0A=1 */
	TPM2C0SC = 0x24U;                    /* Int. flag clearing (2nd part) and channel 0 contr. register setting */
	TPM2C0V = green_;                   /* Compare 0 value setting */
	/* TPM2SC: TOF=0,TOIE=0,CPWMS=0,CLKSB=0,CLKSA=0,PS2=0,PS1=0,PS0=0 */
	TPM2SC = 0x00U;                      /* Stop and reset counter */
	TPM2MOD = 0x04DDU;                   /* Period value setting */
	(void)(TPM2SC == 0U);                /* Overflow int. flag clearing (first part) */
	/* TPM2SC: TOF=0,TOIE=0,CPWMS=1,CLKSB=0,CLKSA=1,PS2=0,PS1=1,PS0=1 */
	TPM2SC = 0x2BU;                      /* Int. flag clearing (2nd part) and timer control register setting */	
}


void process_color(uint8_t red, uint8_t green, uint8_t blue)
{
	process_red(red);
	process_green(green);
	process_blue(blue);
}
